.pragma library

function getMinHeight(height){
    return height < 91 ? 90 : height;
}

function extractImageUrl(description){
    if(description.indexOf('src="') === -1){return 'resources/icon_list.png';}
    var imgUrlStart = description.indexOf('src="') + 5;
    var imgUrlEnd = description.indexOf('" class="', imgUrlStart + 1);
    return description.substr(imgUrlStart, imgUrlEnd - imgUrlStart);
}

function extractDate(dateValue){
    var date = new Date(dateValue);
    return date.getDate() + "/" + ( date.getMonth() + 1 ) + "/" + date.getFullYear();
}

function formatContent(content){
    return removeGoogleAds(replaceYouTubeEmbed(content));
}

function replaceYouTubeEmbed(content){
    if(content.indexOf('http://www.youtube.com/embed/') === -1){return content;}
    var ytvIdStart = content.indexOf('http://www.youtube.com/embed/') + 29;
    var ytvIdEnd = content.indexOf('?', ytvIdStart + 1);
    var ytvId = content.substr(ytvIdStart, ytvIdEnd - ytvIdStart);
    var iframeStart = content.indexOf('<iframe');
    var iframeEnd = content.indexOf('</iframe>', iframeStart + 7) + 9;
    var iframe = content.substr(iframeStart, iframeEnd - iframeStart);
    return content.replace(iframe,'<a href="http://www.youtube.com/watch?v=' + ytvId + '"><img src="http://img.youtube.com/vi/' + ytvId + '/0.jpg" /></a><br /><a href="http://www.youtube.com/watch?v=' + ytvId + '"><img src="resources/youtube.png" /></a>');
}

function removeGoogleAds(content){
    if(content.indexOf('feedads.g.doubleclick.net') === -1){return content;}
    return content.substring(content.indexOf('</p>') + 4);
}

function urlIsExternal(url){
  return (url.toString().indexOf('http') !== -1);
}
